/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sv.com.intesal.rrhh.managed;

import java.io.Serializable;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import sv.com.intesal.clases.LimpiarManaged;
import sv.com.intesal.clases.Mensaje;
import sv.com.intesal.rrhh.bean.Area;
import sv.com.intesal.rrhh.controlador.AreaControlador;
import sv.com.intesal.seguridad.managed.SessionManaged;

/**
 *
 * @author JM
 */
@ManagedBean(name = "areaManaged")
@ViewScoped
public class AreaManaged implements Serializable {

    private Area selectedArea;
    private List<Area> areas;
    private Mensaje m;
    private SessionManaged sm;

    public AreaManaged() {
        selectedArea = new Area();
        m = new Mensaje();
        sm = new SessionManaged();
    }

    public void fillGrid() {
        try {
            if (sm.validar_rol_usuario("wfrmArea", sm.buscar) == true) {
                if (areas == null) {
                    AreaControlador ac = new AreaControlador();
                    areas = ac.fillGrid();
                }
            }
        } catch (Exception ex) {
            m.messageError(m.getIerror());
            ex.printStackTrace();
        }
    }

    public void AreaMantenimiento() {

        try {
            if (validar() == true) {
                if (this.selectedArea.getCodigo() == 0) {
                    if (sm.validar_rol_usuario("wfrmArea", sm.agregar) == true) {

                        AreaControlador ac = new AreaControlador();
                        ac.mantAreaIM(this.selectedArea);
                        LimpiarManaged.limpiar();
                    }
                } else {
                    if (sm.validar_rol_usuario("wfrmArea", sm.modificar) == true) {
                        AreaControlador ac = new AreaControlador();
                        ac.mantAreaIM(this.selectedArea);
                        LimpiarManaged.limpiar();
                    }
                }
            }
        } catch (Exception e) {
            m.messageError(m.getIerror());
            e.printStackTrace();
        }
    }

    private Boolean validar() throws Exception {

        try {
            if (this.selectedArea.getNombre().equals("")) {
                m.messageAdvertencia(m.campoError("AREA DE TRABAJO"));
                return false;
            } else {
                return true;
            }
        } catch (Exception e) {
            throw new Exception(e);
        }
    }

    /*SETTERS AND GETTERS*/
    public List<Area> getAreas() {
        return areas;
    }

    public void setAreas(List<Area> areas) {
        this.areas = areas;
    }

    public Area getSelectedArea() {
        if (selectedArea == null) {
            this.selectedArea = new Area();
        }
        return selectedArea;
    }

    public void setSelectedArea(Area selectedArea) {
        this.selectedArea = selectedArea;
    }
}
